// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// There is no DOMContentLoaded event on restoring from the cache.
function cacheRestoreNavigationOrder(prefix) {
  return [ prefix + "onBeforeNavigate",
           prefix + "onCommitted",
           prefix + "onCompleted" ];
}

onload = async function() {
  let tab = await promise(chrome.tabs.create, {"url": "about:blank"});
  let config = await promise(chrome.test.getConfig);
  let port = config.testServer.port;

  var URL_A = "http://a.com:" + port +
        "/extensions/api_test/webnavigation/backForwardCache/a.html";
  var URL_B = "http://b.com:" + port +
        "/extensions/api_test/webnavigation/backForwardCache/b.html";

  chrome.test.runTests([
    // First navigates to a.html which redirects to to b.html which uses
    // history.back() to navigate back to a.html
    function forwardBack() {
      expect([
        { label: "a-onBeforeNavigate",
          event: "onBeforeNavigate",
          details: { frameId: 0,
                     parentFrameId: -1,
                     processId: -1,
                     tabId: 0,
                     timeStamp: 0,
                     url: URL_A }},
        { label: "a-onCommitted",
          event: "onCommitted",
          details: { documentId: 1,
                     frameId: 0,
                     parentFrameId: -1,
                     processId: 0,
                     tabId: 0,
                     timeStamp: 0,
                     transitionQualifiers: [],
                     transitionType: "link",
                     url: URL_A }},
        { label: "a-onDOMContentLoaded",
          event: "onDOMContentLoaded",
          details: { documentId: 1,
                     frameId: 0,
                     parentFrameId: -1,
                     processId: 0,
                     tabId: 0,
                     timeStamp: 0,
                     url: URL_A }},
        { label: "a-onCompleted",
          event: "onCompleted",
          details: { documentId: 1,
                     frameId: 0,
                     parentFrameId: -1,
                     processId: 0,
                     tabId: 0,
                     timeStamp: 0,
                     url: URL_A }},
        { label: "b-onBeforeNavigate",
          event: "onBeforeNavigate",
          details: { frameId: 0,
                     parentFrameId: -1,
                     processId: -1,
                     tabId: 0,
                     timeStamp: 0,
                     url: URL_B }},
        { label: "b-onCommitted",
          event: "onCommitted",
          details: { documentId: 2,
                     frameId: 0,
                     parentFrameId: -1,
                     processId: 1,
                     tabId: 0,
                     timeStamp: 0,
                     transitionQualifiers: [],
                     transitionType: "link",
                     url: URL_B }},
        { label: "b-onDOMContentLoaded",
          event: "onDOMContentLoaded",
          details: { documentId: 2,
                     frameId: 0,
                     parentFrameId: -1,
                     processId: 1,
                     tabId: 0,
                     timeStamp: 0,
                     url: URL_B }},
        { label: "b-onCompleted",
          event: "onCompleted",
          details: { documentId: 2,
                     frameId: 0,
                     parentFrameId: -1,
                     processId: 1,
                     tabId: 0,
                     timeStamp: 0,
                     url: URL_B }},
        { label: "c-onBeforeNavigate",
          event: "onBeforeNavigate",
          details: { frameId: 0,
                     parentFrameId: -1,
                     processId: -1,
                     tabId: 0,
                     timeStamp: 0,
                     url: URL_A }},
        { label: "c-onCommitted",
          event: "onCommitted",
          details: { // documentId is the same as the first navigation
                     // because the document was restored from the cache.
                     documentId: 1,
                     frameId: 0,
                     parentFrameId: -1,
                     processId: 0,
                     tabId: 0,
                     timeStamp: 0,
                     transitionQualifiers: ["forward_back"],
                     transitionType: "link",
                     url: URL_A }},
        { label: "c-onCompleted",
          event: "onCompleted",
          details: { documentId: 1,
                     frameId: 0,
                     parentFrameId: -1,
                     processId: 0,
                     tabId: 0,
                     timeStamp: 0,
                     url: URL_A }}],
             [ navigationOrder("a-"), navigationOrder("b-"),
               cacheRestoreNavigationOrder("c-"),
               isLoadedBy("b-", "a-"), isLoadedBy("c-", "b-")]);
      chrome.tabs.update(tab.id, { url: URL_A });
    },
  ]);
};
